# -*-coding:utf-8-*-
# -*- coding: utf-8 -*-
# @Time    : 2022/10/14 11:43
# @Author  : gaojy
# @Site    : 
# @File    : common.py
# @Software: PyCharm
import csv
import os

import yaml


def get_parent_path():
    path = os.path.dirname(os.path.dirname(__file__))
    return path


def read_csv(path):
    with open(get_parent_path() + path, "r+", encoding = "utf-8") as f:
        value = csv.reader(f)
        csv_list = []
        result = [data for data in value]
        for data in result:
            csv_list.append(dict(zip(result[0], data)))
        return csv_list[1:]


def read_extract_yaml(key):
    with open(get_parent_path() + "/extract.yaml", encoding = "utf-8") as f:
        data = yaml.load(f.read(), yaml.FullLoader)
        if data:
            if key in data:
                return data[key]


# 读取配置文件yaml，并返回一级key
def read_conf_yaml(one_key, two_key):
    with open(get_parent_path() + "/conf/config_info.yaml", encoding = "utf-8") as f:
        data = yaml.load(f.read(), yaml.FullLoader)
        return data[one_key][two_key]


# 读取配置文件config.yaml，并根据key返回值
def read_config_yaml(key):
    with open(get_parent_path() + "/config.yaml", encoding = "utf-8") as f:
        data = yaml.load(f.read(), yaml.FullLoader)
        return data[key]


# 获取当前系统的代码
def get_sys_code():
    with open(get_parent_path() + "/syscode.yaml", encoding = "utf-8") as f:
        data = yaml.load(f.read(), yaml.FullLoader)
        return data


# 写入extract文件的yaml
def write_extract_yaml(data_dict: dict):
    with open(get_parent_path() + "/extract.yaml", 'a', encoding = 'utf-8') as f:
        yaml.dump(data_dict, stream = f, allow_unicode = True)


# 写入syscode文件的yaml
def write_sys_code_yaml(data):
    with open(get_parent_path() + "/syscode.yaml", 'a', encoding = 'utf-8') as f:
        yaml.dump(data, stream = f, allow_unicode = True)


# 写入config文件的yaml
def write_config_yaml(data_dict: dict):
    with open(get_parent_path() + "/config.yaml", 'a', encoding = 'utf-8') as f:
        yaml.dump(data_dict, stream = f, allow_unicode = True)


# 清理系统编码
def clear_sys_code_yaml():
    with open(get_parent_path() + "/syscode.yaml", 'w', encoding = 'utf-8') as f:
        f.truncate()


# 清理config.yaml
def clear_config_yaml():
    with open(get_parent_path() + "/config.yaml", 'w', encoding = 'utf-8') as f:
        f.truncate()



